//
//  WmcBaseVM.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/1.
//

import RxRelay
import UIKit
import YPBedrock

extension WmcBaseVM {
    enum RefreshState {
        case end
        case noMore
    }
}

// MARK: - [WmcBaseVM]

open class WmcBaseVM: NSObject {
    // Lifecycle

    override public required init() {
        super.init()
        config()
    }

    deinit {
        WmcLogManager.DebugLog(type: .delloc, log: "ViewModel被释放\(self)")
    }

    // Internal

    /// 事务处理线程
    lazy var transactionQueue: DispatchQueue = {
        var queue: DispatchQueue = .init(label: "wmc.queue.\(Self.self)")
        return queue
    }()

    // MARK: 入参

    /// 标题
    var title: String?
    /// 推出模式
    var modalPresentationStyle: UIModalPresentationStyle = .overFullScreen

    // MARK: 输出

    /// 刷新状态信号量
    let refreshRelay: BehaviorRelay<RefreshState> = .init(value: .end)

    // MARK: 回调

    /// 某个页面准备出现
    var viewWillAppearBlock: YPBlock.Empty?
    /// 某个页面完全出现
    var viewDidAppearBlock: YPBlock.Empty?
    /// 某个页面即将消失
    var viewWillDisappearBlock: YPBlock.Empty?
    /// 某个页面已经消失
    var viewDidDisappearBlock: YPBlock.Empty?
    /// 某个页面已经消失
    var viewControllerBackBlock: YPBlock.Empty?

    // MARK: 其他

    var page: Int = 1
    var pageSize: Int = 40

    // MARK: 输入

    /// 绑定刷新状态
    func bindRefreshState(to scrollView: UIScrollView) {
        refreshRelay.skip(1).subscribe(onNext: { state in
            scrollView.mj_header?.endRefreshing()
            switch state {
            case .end:
                scrollView.mj_footer?.endRefreshing()
            case .noMore:
                scrollView.mj_footer?.endRefreshingWithNoMoreData()
            }
        }).disposed(by: rx.disposeBag)
    }

    func config() {}
}
